/*
 * Main.java
 *
 * Created on June 17, 2008, 4:20 PM
 */

package ui;

import core.GameEngine;
import core.KeyboardHandler;
import constants.*;
import java.awt.Dimension;
import java.awt.Toolkit;


/**
 *
 * @author  lakesh
 */
public class Main extends javax.swing.JFrame {         
    private GameEngine reversi;    
    private ui.GameCanvas gameCanvas;

    /** Creates new form Main */
    public Main() {
        initComponents();                
        reversi = new GameEngine();
        gameCanvas = new GameCanvas(reversi,this);        
        canvasPanel.add(gameCanvas);  
        this.setSize(FrameSize.WIDTH, FrameSize.HEIGHT);
        KeyboardHandler kh = new KeyboardHandler(this);
        gameCanvas.addKeyListener(kh);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int xLocation = (int)(d.getWidth() - FrameSize.WIDTH)/2;
        int yLocation = (int)(d.getHeight() - FrameSize.HEIGHT)/2;
        setLocation(xLocation, yLocation);
    }
    
    public  void newgame() {                                
        gameCanvas.reset();
    }   
    
    
    public GameEngine getGameEngine() {
        return reversi;
    }
    
    public void setMoveLabel(String moveLabelText){
        moveLabel.setText(moveLabelText);
    }
    
    public void setScore(int white, int black){
        whiteScore.setText(Integer.toString(white));
        blackScore.setText(Integer.toString(black));
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        help = new javax.swing.JDialog(this);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        canvasPanel = new javax.swing.JPanel();
        scorePanel = new javax.swing.JPanel();
        moveLabel = new javax.swing.JLabel();
        whiteScore = new javax.swing.JLabel();
        black = new javax.swing.JLabel();
        white = new javax.swing.JLabel();
        blackScore = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        help.setTitle("Help");
        help.setMinimumSize(new java.awt.Dimension(400, 300));
        help.setResizable(false);

        jLabel1.setText("How to play Reversi ??");

        jLabel2.setText("Controls");

        jLabel3.setText("Right Mouse Click -> Pass move");

        jLabel4.setText("N -> New Game");

        jLabel5.setText("Q -> Quit");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setText("Reversi  is an abstract strategy board game which involves play by two parties on an eight-by-eight square grid with pieces that have two distinct sides. Pieces typically appear coin-like,with a light and a dark face, each side representing one player. The goal for each player is to make pieces of their color constitute a majority of the pieces on the board at the end of the game, by turning over as many of their opponent's pieces as possible.");
        jTextArea1.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout helpLayout = new javax.swing.GroupLayout(help.getContentPane());
        help.getContentPane().setLayout(helpLayout);
        helpLayout.setHorizontalGroup(
            helpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(helpLayout.createSequentialGroup()
                .addGroup(helpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(helpLayout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(helpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)))
                    .addGroup(helpLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(helpLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(helpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(helpLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1))))
                .addContainerGap(138, Short.MAX_VALUE))
        );
        helpLayout.setVerticalGroup(
            helpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(helpLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addContainerGap(77, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout canvasPanelLayout = new javax.swing.GroupLayout(canvasPanel);
        canvasPanel.setLayout(canvasPanelLayout);
        canvasPanelLayout.setHorizontalGroup(
            canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 485, Short.MAX_VALUE)
        );
        canvasPanelLayout.setVerticalGroup(
            canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 486, Short.MAX_VALUE)
        );

        moveLabel.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        moveLabel.setForeground(new java.awt.Color(0, 0, 0));
        moveLabel.setText("Your turn");

        whiteScore.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        whiteScore.setForeground(new java.awt.Color(255, 102, 102));

        black.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        black.setForeground(new java.awt.Color(0, 0, 0));
        black.setText("Black");

        white.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        white.setForeground(new java.awt.Color(0, 0, 0));
        white.setText("White");

        blackScore.setFont(new java.awt.Font("DejaVu Sans", 1, 12)); // NOI18N
        blackScore.setForeground(new java.awt.Color(255, 102, 102));

        jButton1.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jButton1.setForeground(new java.awt.Color(0, 0, 0));
        jButton1.setText("?");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout scorePanelLayout = new javax.swing.GroupLayout(scorePanel);
        scorePanel.setLayout(scorePanelLayout);
        scorePanelLayout.setHorizontalGroup(
            scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, scorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(moveLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(white)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(whiteScore, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(black)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(blackScore, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jButton1)
                .addContainerGap())
        );
        scorePanelLayout.setVerticalGroup(
            scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(blackScore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(black, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(whiteScore, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
                    .addGroup(scorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(white, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
                        .addComponent(moveLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE))))
            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(canvasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(scorePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(canvasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scorePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    jTextArea1.setLineWrap(true);
    int x = this.getX() + (this.getWidth() - help.getWidth())/2;
    int y = this.getY() + (this.getHeight() - help.getHeight())/2;
    help.setLocation(x,y);    
    help.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);                
            }
        });
    }
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel black;
    private javax.swing.JLabel blackScore;
    private javax.swing.JPanel canvasPanel;
    private javax.swing.JDialog help;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel moveLabel;
    private javax.swing.JPanel scorePanel;
    private javax.swing.JLabel white;
    private javax.swing.JLabel whiteScore;
    // End of variables declaration//GEN-END:variables

}
